/**
 * @File AccountingCalendarController.java


 * @author Agam Bhamra
 * @Version 1.0
 * @Date July 31, 2013
 * 
 * Copyright (c) 2011 EdRP, Dayalbagh Educational Institute.
 * All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 *
 * Redistributions of source code must retain the above copyright
 * notice, this  list of conditions and the following disclaimer.
 *
 * Redistribution in binary form must reproducuce the above copyright
 * notice, this list of conditions and the following disclaimer in
 * the documentation and/or other materials provided with the
 * distribution.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL ETRG OR ITS CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL,SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Contributors: Members of EdRP, Dayalbagh Educational Institute
 */
package in.ac.dei.edrp.generalLedger.accountingCalendar;

import flex.messaging.FlexContext;



import flex.messaging.FlexSession;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.flex.remoting.RemotingDestination;
import org.springframework.stereotype.Controller;

@Controller
@RemotingDestination("accountingCalendarController")


public class AccountingCalendarController {
	@Autowired
	@Qualifier("AccountingCalendarDaoImpl")
	
	private AccountingCalendarDao accountingCalendarDao;
	
	public List<AccountingCalendarInfoBean> getEntries(Map<String, String> obj){
		System.out.println("get entries Started");
		AccountingCalendarInfoBean input = new AccountingCalendarInfoBean();
		input.setCalendarName(obj.get("calendarName"));
		input.setType(obj.get("type"));
		List<AccountingCalendarInfoBean> entryList = new ArrayList<AccountingCalendarInfoBean>();
		entryList = accountingCalendarDao.getEntries(input);
		System.out.println("get entries completed");
		

		return entryList;
	}
	
	
	public List<AccountingCalendarInfoBean> checkCalendar(String name){
		List<AccountingCalendarInfoBean> checkCalendar = new ArrayList<AccountingCalendarInfoBean>();
		System.out.println("checkCalendar started");
		checkCalendar = accountingCalendarDao.checkCalendar(name);	
		System.out.println("checkCalendar completed");
		return checkCalendar;
		
	}
	
	public List<AccountingCalendarInfoBean> getPeriodType(){
		String status = "";
		List<AccountingCalendarInfoBean> getMaxEntry = new ArrayList<AccountingCalendarInfoBean>();
		System.out.println("getMaxEntry started");
		getMaxEntry = accountingCalendarDao.getMaxEntry();	
		System.out.println("getMaxEntry completed" + status);
		return getMaxEntry;
		
	}
	
	public String deletePeriod(Map<String, String> obj){
		String status = "";		
		System.out.println("deletePeriod Started");
		AccountingCalendarInfoBean input = new AccountingCalendarInfoBean();
		input.setCalendarName(obj.get("calendarName"));
		input.setType(obj.get("type"));
		input.setYear(obj.get("year"));
		input.setNumber(obj.get("number"));	
		status = accountingCalendarDao.deletePeriod(input);	
		System.out.println("deletePeriod completed" + status);
		return status;
		
	}
	
	
	public String addCalendar(Map<String, String> obj){
		String status = "";		
		System.out.println("addcalendar Started");
		FlexSession session = FlexContext.getFlexSession();
		AccountingCalendarInfoBean input = new AccountingCalendarInfoBean();
		input.setUserId(session.getAttribute("userId").toString());
		input.setCalendarName(obj.get("calendarName"));
		input.setCalendarDescription(obj.get("calendarDescription"));
		status = accountingCalendarDao.addcalendar(input);	
		System.out.println("addcalendar completed" + status);
		return status;
		
	}
	
	
	public List<AccountingCalendarInfoBean> calendarList(){
		List<AccountingCalendarInfoBean> checkCalendar = new ArrayList<AccountingCalendarInfoBean>();
		System.out.println("calendarList started");
		checkCalendar = accountingCalendarDao.calendarList();	
		System.out.println("calendarList completed");
		return checkCalendar;
		
	}
	
	
	public String checkOverlapping(Map<String, String> obj){
		String status = "";		
		System.out.println("checkOverlapping Started");
		AccountingCalendarInfoBean input = new AccountingCalendarInfoBean();
		System.out.println("3");
		Date date= new Date(obj.get("fromDate"));
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		System.out.println("2");
		input.setFromDate((sdf.format(date)));
		System.out.println("1");
		date= new Date(obj.get("toDate"));
		input.setToDate((sdf.format(date)));
		input.setCalendarName(obj.get("calendarName"));
		input.setType(obj.get("type"));
		input.setNumber(obj.get("number"));
		input.setYear(obj.get("year"));
		System.out.println("checkOverlapp   " + input.getFromDate());
		status = accountingCalendarDao.checkOverlapping(input);	
		System.out.println("checkOverlapping completed" + status);
		return status;
	}
	
	public String addEntries (List<Map<String, String>> add)
	{
		String status = "";
		AccountingCalendarInfoBean input = new AccountingCalendarInfoBean();
		status=accountingCalendarDao.addEntries(add, input);	
		return status;
	}
	
	public String editEntries (List<Map<String, String>> edit)
	{
		String status = "";
		AccountingCalendarInfoBean input = new AccountingCalendarInfoBean();
		status =accountingCalendarDao.editEntries(edit, input);	
		return status;
	}
	
}
